package com.puhui.lib.utils

import android.content.ClipData
import android.content.ClipboardManager
import android.content.Context
import android.content.pm.PackageInfo
import android.content.pm.PackageManager
import android.text.TextUtils
import android.util.Base64
import com.blankj.utilcode.util.AppUtils
import java.security.MessageDigest
import java.security.NoSuchAlgorithmException

fun getCopy(context: Context): String? {
    // 获取系统剪贴板
    val clipboard = context.getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager
    // 返回数据
    val clipData = clipboard.primaryClip
    return if (clipData != null && clipData.itemCount > 0) {
        // 从数据集中获取（粘贴）第一条文本数据
        clipData.getItemAt(0).text.toString()
    } else null
}

fun copyString(source: String?, context: Context): String? {
    if (TextUtils.isEmpty(source)) return null

    val clipboardManager = context.getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager
    try {
        clipboardManager.setPrimaryClip(ClipData.newPlainText(null, source))
        return source
    } catch (e: Exception) {
        e.printStackTrace()
    }
    return null
}

/**
 * 获取密钥散列  facebook开放平台需要
 * 当前APP的秘钥散列：debug:   vRKDdO9ubUvcBuPRZmsJQziEM3Q=
 * *                release: M+yiF7e1DCo0KZoUXCC6XBR6XkI=
 */
fun getSignatureInfo(context: Context) {
    try {
        val info: PackageInfo = context.packageManager.getPackageInfo(
            AppUtils.getAppPackageName(), PackageManager.GET_SIGNATURES
//            AppUtils.getAppPackageName(), PackageManager.GET_SIGNING_CERTIFICATES  //api28+
        )
        for (signature in info.signatures) {
            val md: MessageDigest = MessageDigest.getInstance("SHA")
            md.update(signature.toByteArray())
            LogUtils.printE("KeyHash:${Base64.encodeToString(md.digest(), Base64.DEFAULT)}")
        }
    } catch (e: PackageManager.NameNotFoundException) {
    } catch (e: NoSuchAlgorithmException) {
    }
}

/**
 * 判断集合是否为空
 */
fun isListEmpty(list: List<Any>?): Boolean {
    return null == list || list.isEmpty()
}

/**
 * 判断集合是否为空
 */
fun isMutableListEmpty(list: MutableList<out Any>?): Boolean {
    return null == list || list.isEmpty()
}